package com.feib.soeasy.dao.hibernate;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.PayerDao;
import com.feib.soeasy.model.Payer;
import com.feib.soeasy.pagination.PaginatedList;

/**
 * @title (#)PayerHibernateDao.java<br>
 * @description <br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/2/6 create by Anson Tsai<br>
 */
@Repository("payerDao")
public class PayerHibernateDao extends BaseHibernateDao<Payer, Long> implements PayerDao {

    /*
     * (non-Javdoc)
     * 
     * @see
     * com.feib.soeasy.dao.PayerDao#queryPaginationByConditions(java.lang.String
     * , java.lang.String, java.lang.String, java.lang.String, java.lang.String,
     * com.feib.soeasy.pagination.PaginatedList)
     */
    @Override
    @SuppressWarnings("unchecked")
    public PaginatedList<Payer> queryPaginationByConditions(String groupNo, String payerNo, String payerName, String payerPhone, String houseNumber, String payerMobile, PaginatedList<Payer> paginationConfig) {
        PaginatedList<Payer> result = new PaginatedList<Payer>();
        result.setPageSize(paginationConfig.getPageSize());
        result.setPageNumber(paginationConfig.getPageNumber());
        result.setSortCriterion(paginationConfig.getSortCriterion());
        result.setSortDirection(paginationConfig.getSortDirection());

        result.setFullListSize(super.countByDetachedCriteria(
                        createDetachedCriteriaByConditions(groupNo, payerNo, payerName, payerPhone, houseNumber, payerMobile)));

        // 筆數大於0
        if (0 < result.getFullListSize()) {
            DetachedCriteria criteria =
                            createDetachedCriteriaByConditions(groupNo, payerNo , payerName, payerPhone, houseNumber, payerMobile);

            // 排序
            if (null != paginationConfig.getSortCriterion()) {
                // 由小到大
                if (PaginatedList.SORT_DIRECTION_ASC == paginationConfig.getSortDirection()) {
                    criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                }
                else // 由大到小
                {
                    criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                }
            }

            int maxResults = result.getPageSize(); // the maximum number of
                                                   // results
            int firstResult = result.getFirstIndexForCurrentPage(); // the first
                                                                    // result to
                                                                    // retrieve,
                                                                    // numbered
                                                                    // from 0
            result.setPaginatedList(this.getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults));
        }

        return result;
    }

    protected DetachedCriteria createDetachedCriteriaByConditions(String groupNo, String payerNo, String payerName, String payerPhone, String houseNumber, String payerMobile) {
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);

        if (StringUtils.isNotBlank(groupNo)) {
            DetachedCriteria groupCriteria = criteria.createCriteria("group");
            groupCriteria.add(Restrictions.eq("groupNo", groupNo));
        }

        if (StringUtils.isNotBlank(payerNo)) {
            criteria.add(Restrictions.like("payerNo", payerNo, MatchMode.START));
        }

        if (StringUtils.isNotBlank(payerName)) {
            criteria.add(Restrictions.like("payerName", payerName, MatchMode.START));
        }

        if (StringUtils.isNotBlank(payerPhone)) {
            criteria.add(Restrictions.like("payerTelNo", payerPhone, MatchMode.START));
        }

        if (StringUtils.isNotBlank(houseNumber)) {
            criteria.add(Restrictions.like("houseNo", houseNumber, MatchMode.START));
        }

        if (StringUtils.isNotBlank(payerMobile)) {
            criteria.add(Restrictions.like("payerCellNo", payerMobile, MatchMode.START));
        }

        return criteria;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Payer getPayer(String groupNo, String payerNo) {
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);

        if (StringUtils.isNotBlank(groupNo)) {
            DetachedCriteria groupCriteria = criteria.createCriteria("group");
            groupCriteria.add(Restrictions.eq("groupNo", groupNo));
        }

        if (StringUtils.isNotBlank(payerNo)) {
            criteria.add(Restrictions.eq("payerNo", payerNo));
        }

        List<Payer> list = this.getHibernateTemplate().findByCriteria(criteria);

        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

}
